{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python37564bitd06b180cb4c24e61a874644bd0beb782",
   "display_name": "Python 3.7.5 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import mindquantum as mq\n",
    "from mindquantum.core import X, Y, Z, H, RX, RY, RZ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Gate name: X\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 1],\n       [1, 0]])"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "print('Gate name:', X)  \n",
    "X.matrix()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Gate name: Y\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 0.+0.j, -0.-1.j],\n       [ 0.+1.j,  0.+0.j]])"
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "print('Gate name:', Y)  \n",
    "Y.matrix()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Gate name: Z\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 1,  0],\n       [ 0, -1]])"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "print('Gate name:', Z)  \n",
    "Z.matrix()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Gate name: H\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 0.70710678,  0.70710678],\n       [ 0.70710678, -0.70710678]])"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "print('Gate name:', H)  \n",
    "H.matrix()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "X(0 <-: 1)\n"
    }
   ],
   "source": [
    "cnot = X.on(0, 1)                  #X门作用在第0位量子比特且受第1位量子比特控制  \n",
    "print(cnot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Gate name: RX(theta)\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[1.+0.j, 0.+0.j],\n       [0.+0.j, 1.+0.j]])"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "rx = RX('theta')  \n",
    "print('Gate name:', rx)  \n",
    "rx.matrix({'theta': 0})            #赋予theta的值为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Gate name: RY(theta)\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[ 0.70710678+0.j, -0.70710678+0.j],\n       [ 0.70710678+0.j,  0.70710678+0.j]])"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "ry = RY('theta')  \n",
    "print('Gate name:', ry)  \n",
    "ry.matrix({'theta': np.pi/2})      #pi需要从np中导入，赋予theta的值为pi/2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Gate name: RZ(theta)\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[-0.-1.j,  0.+0.j],\n       [ 0.+0.j, -0.+1.j]])"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "rz = RZ('theta')  \n",
    "print('Gate name:', rz)  \n",
    "np.round(rz.matrix({'theta': np.pi}))        #赋予theta的值为pi，由于计算机中存在浮点数不精确的问题，因此通过函数np.round返回浮点数的四舍五入值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "┏━━━┓           \nq0: ──┨ H ┠───■───────\n      ┗━━━┛   ┃       \n            ┏━┻━┓     \nq1: ────────┨╺╋╸┠─────\n            ┗━━━┛     \n      ┏━━━━━━━━━━━┓   \nq2: ──┨ RY(theta) ┠───\n      ┗━━━━━━━━━━━┛   \n"
    },
    {
     "output_type": "display_data",
     "data": {
      "text/plain": "\u001b[1;38;2;255;0;0m        Circuit Summary         \u001b[0m\n╭──────────────────────┬───────╮\n│\u001b[1m \u001b[0m\u001b[1;38;2;59;59;149mInfo\u001b[0m\u001b[1m                \u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1;38;2;59;59;149mvalue\u001b[0m\u001b[1m \u001b[0m│\n├──────────────────────┼───────┤\n│ \u001b[1mNumber of qubit\u001b[0m      │ 3     │\n├──────────────────────┼───────┤\n│ \u001b[1mTotal number of gate\u001b[0m │ 3     │\n│ Barrier              │ 0     │\n│ Noise Channel        │ 0     │\n│ Measurement          │ 0     │\n├──────────────────────┼───────┤\n│ \u001b[1mParameter gate\u001b[0m       │ 1     │\n│ 1 ansatz parameter   │ \u001b[38;2;72;201;176mtheta\u001b[0m │\n╰──────────────────────┴───────╯\n",
      "text/html": "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #ff0000; text-decoration-color: #ff0000; font-weight: bold\">        Circuit Summary         </span>\n╭──────────────────────┬───────╮\n│<span style=\"font-weight: bold\"> </span><span style=\"color: #3b3b95; text-decoration-color: #3b3b95; font-weight: bold\">Info</span><span style=\"font-weight: bold\">                 </span>│<span style=\"font-weight: bold\"> </span><span style=\"color: #3b3b95; text-decoration-color: #3b3b95; font-weight: bold\">value</span><span style=\"font-weight: bold\"> </span>│\n├──────────────────────┼───────┤\n│ <span style=\"font-weight: bold\">Number of qubit</span>      │ 3     │\n├──────────────────────┼───────┤\n│ <span style=\"font-weight: bold\">Total number of gate</span> │ 3     │\n│ Barrier              │ 0     │\n│ Noise Channel        │ 0     │\n│ Measurement          │ 0     │\n├──────────────────────┼───────┤\n│ <span style=\"font-weight: bold\">Parameter gate</span>       │ 1     │\n│ 1 ansatz parameter   │ <span style=\"color: #48c9b0; text-decoration-color: #48c9b0\">theta</span> │\n╰──────────────────────┴───────╯\n</pre>\n"
     },
     "metadata": {}
    }
   ],
   "source": [
    "from mindquantum.core import Circuit          #导入Circuit模块，用于搭建量子线路  \n",
    "\n",
    "encoder = Circuit()                           #初始化量子线路  \n",
    "encoder += H.on(0)                            #H门作用在第0位量子比特  \n",
    "encoder += X.on(1,0)                          #X门作用在第1位量子比特且受第0位量子比特控制  \n",
    "encoder += RY('theta').on(2)                  #RY(theta)门作用在第2位量子比特  \n",
    "\n",
    "print(encoder)                                #打印Encoder  \n",
    "encoder.summary()                             #总结Encoder量子线路"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}